package com.zzmcc.wechat.util;

import com.zzmcc.wechat.entity.Simsimi;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import org.smart4j.framework.util.JsonUtil;

/**
 * 聊天机器人
 */
public class ChatRobot {
	
	private static final String GET_SIMSIMI_URL= "http://sandbox.api.simsimi.com/request.p?key=";
	private static final String SIMSIMI_KEY="5daa0a9f-acc9-4f60-8567-569c4800c502";
	
	public static String getSimimi(String msg) {
		String result =null;
		//创建默认的 HttpClient 实例  
        HttpClient httpClient = new DefaultHttpClient();  
        try {  
            //创建 httpUriRequest 实例  
        	
            //HttpGet httpGet = new HttpGet(GET_SIMSIMI_URL + SIMSIMI_KEY + "&lc=ch&ft=1.0&text=" + msg);
            HttpGet httpGet = new HttpGet("http://dev.skjqr.com/api/weixin.php?email=joinwin@139.com&appkey=f2473544895e9b52c8caf2722eb1d700&msg="+msg);
            //System.out.println("uri=" + httpGet.getURI());
            //执行 get 请求  
            HttpResponse httpResponse = httpClient.execute(httpGet);  
            //获取响应实体  
            HttpEntity httpEntity = httpResponse.getEntity();  
            //打印响应状态  
            System.out.println(httpResponse.getStatusLine());  
            if (httpEntity != null) {  
                //响应内容的长度  
                long length = httpEntity.getContentLength();  
                //响应内容  
                result = EntityUtils.toString(httpEntity);
               // System.out.println("Response content length:" + length);
               // System.out.println("Response content:" + content);
               // Simsimi simsimi = JsonUtil.fromJSON(content, Simsimi.class);
               // result = simsimi.getResponse();
                if (result==null)
                {	
                	int ram=(int)(Math.random()*6)+1 ;
                	switch(ram)
                	{
	                	case 1: result="小鸡鸡今天累了，明天再陪你聊了";
	                	case 2: result="小鸡鸡睡觉了~~";
	                	default: result="你话太多了，不陪你聊了";
                	}
                }
                
            }              
            //有些教程里没有下面这行  
            httpGet.abort();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            //关闭连接，释放资源  
            httpClient.getConnectionManager().shutdown();  
        }
        return result;
	
	}
    
	public static void main(String args[]){
		String result = ChatRobot.getSimimi("你好");
		
		System.out.println("result:"+result);
		
	}
}